var express = require('express');
var router = express.Router();
const sql = require("../sql/sql");//引入增删改查命令
const banner = require("../sql/banner");//引入数据集合

/* GET home page. */
//获取所有banner信息的接口路由
router.get('/', function(req, res, next) {
  sql.find(banner,{},{_id:0,__v:0}).then((data)=>{
    res.send({
      state:200,
      title:"请求成功",
      data
    })
  }).catch(()=>{
    res.send({
      state:201,
      title:"请求失败",
      data:"NOT DATA"
    })
  })
});

/**
* @apiGroup banner信息
* @api {get} /api/banner 获取所有banner数据
* @apiVersion 0.0.0
* @apiDescription 获取banner数据
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {array} data 请求数据
* @apiSuccessExample {json} Success-Response:
* {
*    "state":200,
*    "title":"请求成功",
*    "data":[{},{},...]
* }
* @apiError {number} state 请求状态
* @apiError {string} title 请求说明
* @apiError {string} data 请求数据
* @apiErrorExample {json} Error-Response:
* {
*    "code":201,
*    "title":"请求失败",
*    "data":"NOT DATA"
* }
* @apiSampleRequest http://localhost:3001/api/banner
*/


//获取单个banner信息的接口路由
router.get('/oneBanner', function(req, res, next) {
  const {id} = req.query;
  sql.find(banner,{imgId:id},{_id:0,__v:0}).then((data)=>{
    res.send({
      state:200,
      title:"请求成功",
      data
    })
  }).catch(()=>{
    res.send({
      state:201,
      title:"请求失败",
      data:"NOT DATA"
    })
  })
});


/**
* @apiGroup banner信息
* @api {get} /api/banner/oneBanner 获取单个banner数据
* @apiVersion 0.0.0
* @apiDescription 获取单个banner数据，需要发送banner的id
* @apiParam {string} id banner的id
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {object} data 请求数据
* @apiSuccessExample {json} Success-Response:
* {
*    "state":200,
*    "title":"请求成功",
*    "data":[{},{},...]
* }
* @apiError {number} state 请求状态
* @apiError {string} title 请求说明
* @apiError {string} data 请求数据
* @apiErrorExample {json} Error-Response:
* {
*    "code":201,
*    "title":"请求失败",
*    "data":"NOT DATA"
* }
* @apiSampleRequest http://localhost:3001/api/banner/oneBanner
*/



module.exports = router;
